home *** CD-ROM | disk | FTP | other *** search
- Path: vixen.cso.uiuc.edu!usenet
- From: e-torres@uiuc.edu (Edgar L. Torres)
- Newsgroups: comp.lang.c++
- Subject: HELP: Overloading [] operator for multidimensional array of objects.
- Date: Mon, 25 Mar 1996 18:39:38 GMT
- Organization: University of Illinois at Urbana
- Message-ID: <4j6p2v$dhg@vixen.cso.uiuc.edu>
- NNTP-Posting-Host: berlin-4.slip.uiuc.edu
- Summary: How does one specify overloading the []operator for array of objects?
- Keywords: overload, subscript, multidimensional, arrays, operator
- X-Newsreader: Forte Free Agent 1.0.82
-
- Hi!
-
- I would like to specify the overload of [], such that I may access
- a multidimensional array of objects. As an example, given I have a
- class
- EXAMCLASS and I wish to dynamically allocate a three-dimensional array
- of
- objects:
- EXAMCLASS object1***;
- ...
- object1 = malloc( sizeof(object1), dim1sz * dim2sz * dim3sz );
- I know the above is sacrilege to C++ purists, but bare with me, since
- the malloc
- does work correctly in C++. Now, what I want is to be able to:
- object1[arg1][arg2][arg3].obj1Var1 = something;
- OR
- something = object1[arg1][arg2][arg3].obj1Var1;
- In general, I know I have to include three friend operator in the
- definition of
- class EXAMCLASS, and then define them outside:
- EXAMCLASS** operator[](EXAMCLASS*** C, int i){
- return ((EXAMCLASS **)(C + (i * dim2sz * dim3sz))); }
- EXAMCLASS* operator[](EXAMCLASS** C, int i){
- return ((EXAMCLASS *)(C + (i * dim3sz))); }
- EXAMCLASS operator[](EXAMCLASS* C, int i){
- return *(C + i); }
- I know that the above does not work, but it does present the basic
- idea of what
- I am trying to do. If anybody has the knowledge to help with the
- specifics of
- syntax and argument rules, please HELP ME! Somebody suggested
- replacing the *
- with &, but I am not sure what EXAMCLASS&& means??
-
- Thanks in advance,
- Edgar
-
-
-